/**
 * Created by dmrj on 2016/5/16.
 */
define(['jquery', 'model/global', 'appMessageBox', 'appConst'], function ($, global, msgBox, appConst) {
    return {
        searchProjectsNotInThisGroup: function (params, callback, errCallBack) {
            $.ajax({
                url: 'group/findProjectsForNotInThisGroup',
                type: 'POST',
                dataType: 'JSON',
                "data": params,
                success: function (d) {
                    if (d.code == 0) {
                        msgBox.errorBox(d.message);
                        if (typeof errCallBack == 'function') {
                            errCallBack();
                        }
                    } else {
                        if (typeof callback == 'function') {
                            callback(d.body);
                        }
                    }
                },
                error: function (e) {
                    if (e.status == 401) {
                        msgBox.sessionFailureBox();
                    }
                    if (e.status == 500) {
                        msgBox.serverFailureBox();
                    }
                    if (e.status == 404) {
                        msgBox.pathFailureBox();
                    }
                    if (typeof errCallBack == 'function') {
                        errCallBack();
                    }
                }
            });
        },
        searchUsersNotInThisGroup: function (params, callback, errCallBack) {
            $.ajax({
                url: 'group/findUsersForNotInThisGroup',
                type: 'POST',
                dataType: 'JSON',
                "data": params,
                success: function (d) {
                    if (d.code == 0) {
                        msgBox.errorBox(d.message);
                        if (typeof errCallBack == 'function') {
                            errCallBack();
                        }
                    } else {
                        if (typeof callback == 'function') {
                            callback(d.body);
                        }
                    }
                },
                error: function (e) {
                    if (e.status == 401) {
                        msgBox.sessionFailureBox();
                    }
                    if (e.status == 500) {
                        msgBox.serverFailureBox();
                    }
                    if (e.status == 404) {
                        msgBox.pathFailureBox();
                    }
                    if (typeof errCallBack == 'function') {
                        errCallBack();
                    }
                }
            });
        },
        createGroup: function (param, callback, errCallBack) {
            $.ajax({
                "url": "group/createGroup",
                "type": "POST",
                "data": param,
                "success": function (d) {
                    if (d.code == 0) {
                        if ($.isFunction(errCallBack)) {
                            errCallBack(d.message);
                        }
                    } else {
                        if ($.isFunction(callback)) {
                            callback(d.body);
                        }
                    }
                },
                "error": function (e) {
                    if (e.status == 401) {
                        msgBox.sessionFailureBox();
                    }
                    if (e.status == 500) {
                        msgBox.serverFailureBox();
                    }
                    if (e.status == 404) {
                        msgBox.pathFailureBox();
                    }
                    if ($.isFunction(errCallBack)) {
                        errCallBack();
                    }
                }
            });
        },
        deleteGroup: function (id, callback, errCallBack) {
            $.ajax({
                "url": "group/delete/" + id,
                "type": "POST",
                "success": function (d) {
                    if (d.code == 0) {
                        msgBox.errorBox(d.message);
                        if ($.isFunction(errCallBack)) {
                            errCallBack(d.message);
                        }
                    } else {
                        if ($.isFunction(callback)) {
                            callback(d.body);
                        }
                    }
                },
                "error": function (e) {
                    if (e.status == 401) {
                        msgBox.sessionFailureBox();
                    }
                    if (e.status == 500) {
                        msgBox.serverFailureBox();
                    }
                    if (e.status == 404) {
                        msgBox.pathFailureBox();
                    }
                    if ($.isFunction(errCallBack)) {
                        errCallBack();
                    }
                }
            });
        }
    }
});